package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.domain.SkuProperty;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.vo.JSONResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类相关
 */
@Controller
@RequestMapping("/catalogs")
public class CatalogController {
    @Reference
    private ICatalogService catalogService;
    @Reference
    private IPropertyService propertyService;
    @Reference
    private ISkuPropertyService skuPropertyService;

    /*
        商品分类树
     */
    @RequestMapping("/view")
    public String viewAllCatalog(Model model){
        List<Catalog> catalogs = catalogService.listAll();
        String allCatalog = JSON.toJSONString(catalogs);
        model.addAttribute("allCatalog",allCatalog);
        return "catalog/catalog";
    }

    @GetMapping("{id}/childs")
    public String listChildCatalog(Model model,@PathVariable("id") Long id){
        List<Catalog> catalogList = catalogService.listByPid(id);
        model.addAttribute("catalogList",catalogList);
        return "catalog/child_catalog";
    }

    /**
     * 更新某个商品分类信息
     */
    @PostMapping
    @ResponseBody
    public JSONResult update(Catalog catalog){
        JSONResult jsonResult = new JSONResult();
        catalogService.saveOrUpdate(catalog);
        return jsonResult;
    }

    /**
     * 拖拽排序
     */
    @PatchMapping
    @ResponseBody
    public JSONResult sort(@RequestBody List<Long> ids){
        JSONResult jsonResult = new JSONResult();
        catalogService.updateSort(ids);
        return jsonResult;
    }

    @DeleteMapping("{id}")
    @ResponseBody
    public JSONResult delete(@PathVariable("id") Long id ){
        JSONResult jsonResult = new JSONResult();
        catalogService.deleteById(id);
        return jsonResult;
    }

    /**
     * 获取某个商品类别下的属性
     */
    @GetMapping("{catalogId}/propertys")
    public String listPropertys(Model model,@PathVariable("catalogId") Long catalogId){
        List<Property> properties = propertyService.listByCatalogId(catalogId);
        model.addAttribute("propertyList",properties);
        return "property/property_list";
    }

    /**
     * 获取某个商品分类下的属性及属性值
     * @param model
     * @param catalogId
     * @return
     */
    @GetMapping("{catalogId}/propertys/propertyValues")
    public String getProVal(Model model,@PathVariable("catalogId") Long catalogId){
        List<Property> propertyList=propertyService.listPropertysWithValues(catalogId);
        model.addAttribute("propertyList",propertyList);
        return "product/product_property_value";
    }


    /**
     * 获取某个商品类别下的sku属性
     */
    @GetMapping("{catalogId}/skuPropertys")
    public String listSkuPropertys(Model model,@PathVariable("catalogId") Long catalogId){
        List<SkuProperty> properties = skuPropertyService.listByCatalogId(catalogId);
        model.addAttribute("propertyList",properties);
        return "sku_property/property_list";
    }
}
